package org.dromara.chicken.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.chicken.domain.GrowInfo;
import org.dromara.chicken.service.IGrowInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 成长记录
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/farm/chicken/growInfo")
public class GrowInfoController extends BaseController {

    private final IGrowInfoService growInfoService;

    /**
     * 查询成长记录列表
     */
    @SaCheckPermission("chicken/chicken:growInfo:list")
    @GetMapping("/list")
    public TableDataInfo<GrowInfo> list(GrowInfo bo, PageQuery pageQuery) {
        return growInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出成长记录列表
     */
    @SaCheckPermission("chicken/chicken:growInfo:export")
    @Log(title = "成长记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GrowInfo bo, HttpServletResponse response) {
        List<GrowInfo> list = growInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "成长记录", GrowInfo.class, response);
    }

    /**
     * 获取成长记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("chicken/chicken:growInfo:query")
    @GetMapping("/{id}")
    public R<GrowInfo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(growInfoService.queryById(id));
    }

    /**
     * 新增成长记录
     */
    @SaCheckPermission("chicken/chicken:growInfo:add")
    @Log(title = "成长记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GrowInfo bo) {
        return toAjax(growInfoService.insertByBo(bo));
    }

    /**
     * 修改成长记录
     */
    @SaCheckPermission("chicken/chicken:growInfo:edit")
    @Log(title = "成长记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GrowInfo bo) {
        return toAjax(growInfoService.updateByBo(bo));
    }

    /**
     * 删除成长记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("chicken/chicken:growInfo:remove")
    @Log(title = "成长记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(growInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
